package logic

import (
	"context"
	jwt "github.com/golangblogs/gojwt"
	"houseRpc/house"

	"api/internal/svc"
	"api/internal/types"

	"github.com/zeromicro/go-zero/core/logx"
)

type LoginLogic struct {
	logx.Logger
	ctx    context.Context
	svcCtx *svc.ServiceContext
}

func NewLoginLogic(ctx context.Context, svcCtx *svc.ServiceContext) *LoginLogic {
	return &LoginLogic{
		Logger: logx.WithContext(ctx),
		ctx:    ctx,
		svcCtx: svcCtx,
	}
}

func (l *LoginLogic) Login(req *types.LoginRequest) (resp *types.Response, err error) {
	// todo: add your logic here and delete this line

	login, err := l.svcCtx.HouseRpc.Login(l.ctx, &house.LoginRequest{
		Mobile:  req.Mobile,
		SmsCode: req.SmsCode,
	})
	if err != nil {
		return &types.Response{
			Message: err.Error(),
			Code:    500,
			Data:    nil,
		}, nil
	}

	newJwt := jwt.NewJWT("Token")
	claims := jwt.CustomClaims{
		ID: uint(login.Id),
	}

	token, _ := newJwt.CreateToken(claims)

	return &types.Response{
		Message: "登陆成功",
		Code:    200,
		Data:    token,
	}, nil
}
